package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.material.dao.MaterApplyDao;
import com.zhongwang.mms.module.material.model.MAdjustModel;
import com.zhongwang.mms.module.material.service.MateApplyService;
import com.zhongwang.mms.module.material.service.MateApplyCheckService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 库存调整审核 控制层
 * @Author: China.mc
 * @Date: 2020/3/21
 */
@Controller
@RequestMapping("/material/matInvManage/rawMatJusTexaChange")
public class MateApplyCheckController extends BaseController {

    @Autowired
    private MateApplyCheckService mateApplyServiceCheck;
    @Autowired
    private MaterApplyDao materApplyDao;
    @Autowired
    private MateApplyService mateApplyService;


    /**
     * 页面路径
     *
     * @return
     */
    @GetMapping
    @RequiresPermissions({"matinv:matadjapp:view"})
    public String project() {
        return "material/matInvManage/rawMatJusTexaChange/page";
    }

    /**
     * 调整审核加载页面
     *
     * @param madjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/checklist")
    @RequiresPermissions({"matinv:matadjapp:view"})
    public JsonResult checklist(MAdjustModel madjustModel) {
        List<MAdjustModel> checklist = mateApplyServiceCheck.findProjectDailychenck(madjustModel);
        return jsonResult(checklist);
    }

    /**
     * 库存调整申请查看
     *
     * @param adjId
     * @param model
     * @return
     */
    @GetMapping("bbb/{adjId}")
    public String project(@PathVariable("adjId") Integer adjId, Model model) {
        //通过ID查询调整数据，获取生成编码
        MAdjustModel checkSle = materApplyDao.findAdjByOderCode(adjId);
        checkSle.getAdjOrderCode();
        checkSle.getAdjId();
        //将前台绑值字段制空
        MAdjustModel adjust = null;
        MAdjustModel advive = null;
        MAdjustModel advivetow = null;
        //将生成编码号传到后台，进行查询判断
        advive = materApplyDao.SelfindAdjByOderCode(checkSle);
        advivetow = materApplyDao.findAdjByOderCodeTow(checkSle);
        adjust = mateApplyService.findviewtDailyId(checkSle);
        //调整物料与单价时合并操作列进行判断查看
        if (advive != null && advive.getAdjOditStatus() != null && advive.getAdjOditStatus() == 2 && advive.getAdjBeAfFlag() != null && advive.getAdjBeAfFlag() == 1) {
            model.addAttribute("advive", advive);
        }
        //调整物料前的物料信息
        if (advivetow != null && advivetow.getAdjOditStatus() != null && advivetow.getAdjOditStatus() == 2 && advivetow.getAdjBeAfFlag() != null && advivetow.getAdjBeAfFlag() == 2) {
            model.addAttribute("advivetow", advivetow);
        }
        //修改在库数量时进行查看
        if (adjust != null && adjust.getAdjOditStatus() == null && adjust.getAdjOrderCode() == null && adjust.getAdjBeAfFlag() == null) {
            model.addAttribute("adjust", adjust);
        }
        //审核时获取审核时间审核人等信息
        model.addAttribute("check", mateApplyServiceCheck.findviewCheckId(checkSle));
        return "material/matInvManage/rawMatJusTexaChange/view";
    }

    /**
     * 提交申请后，如果有标记了，改库存的在库数量时验证不能审核通过
     *
     * @param madjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/audit")
    @RequiresPermissions({"matinv:matadjapp:edit"})
    public JsonResult audit(MAdjustModel madjustModel) {
        Integer res = mateApplyServiceCheck.updateauditpass(madjustModel);
        if (res == 0) {
            return JsonResult.fail("在库数量与可用数量不等，不能调整该记录。需要先查看可用数量！");
        }
        return JsonResult.success("审核通过！");
    }

    /**
     * 库存审核（批量审核）
     *
     * @param arr 明细Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/togetherAudit")
    @RequiresPermissions({"matinv:matadjapp:edit"})
    public JsonResult togetherAudit(@RequestBody List<Integer> arr) {

        JsonResult jsonResult = new JsonResult();
        String msg = mateApplyServiceCheck.togetherAudit(arr);
        if (msg.contains("审核通过")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 提交审核不通过数据
     *
     * @param madjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/failed")
    @RequiresPermissions({"matinv:matadjapp:edit"})
    public JsonResult failed(MAdjustModel madjustModel) {
        return jsonResult(mateApplyServiceCheck.updateauditfailed(madjustModel));
    }


}



